home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch9 / SpBall.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-05-28  |  1.7 KB  |  62 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "BallSprite"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. ' Bouncing ball sprite.
  15. Option Explicit
  16.  
  17. Implements Sprite
  18.  
  19. Private Radius As Integer
  20. Private Cx As Integer       ' Position of center.
  21. Private Cy As Integer
  22. Private Vx As Integer       ' Velocity.
  23. Private Vy As Integer
  24. Private Color As Long
  25. ' Draw the circle on the indicated picture box.
  26. Public Sub Sprite_DrawSprite(ByVal pic As PictureBox)
  27.     pic.FillColor = Color
  28.     pic.Circle (Cx, Cy), Radius, Color
  29. End Sub
  30.  
  31. ' Initialize the ball.
  32. Public Sub InitializeBall(ByVal new_radius As Integer, ByVal new_cx As Integer, ByVal new_cy As Integer, ByVal new_vx As Integer, ByVal new_vy As Integer, ByVal new_color As Long)
  33.     Radius = new_radius
  34.     Cx = new_cx
  35.     Cy = new_cy
  36.     Vx = new_vx
  37.     Vy = new_vy
  38.     Color = new_color
  39. End Sub
  40. ' Add the velocity components to the sprite's
  41. ' position components.
  42. Public Sub Sprite_MoveSprite(ByVal xmin As Integer, ByVal xmax As Integer, ByVal ymin As Integer, ByVal ymax As Integer)
  43.     Cx = Cx + Vx
  44.     Cy = Cy + Vy
  45.  
  46.     ' Keep the object within the drawing area.
  47.     If (Cx < xmin) Then
  48.         Cx = 2 * xmin - Cx
  49.         Vx = -Vx
  50.     ElseIf (Cx > xmax) Then
  51.         Cx = 2 * xmax - Cx
  52.         Vx = -Vx
  53.     End If
  54.     If (Cy < ymin) Then
  55.         Cy = 2 * ymin - Cy
  56.         Vy = -Vy
  57.     ElseIf (Cy > ymax) Then
  58.         Cy = 2 * ymax - Cy
  59.         Vy = -Vy
  60.     End If
  61. End Sub
  62.